<?php
namespace Usap\Service;
class Service{
	
	static function instance ()
	{
		static $instance = null;
	
		if (is_null($instance)) {
			$instance = new self();
		}
		return $instance;
	}
	

	
	/**
	 * 多级别载入模块
	 * @param unknown $level1_name
	 * @param unknown $level2_name
	 * @param string $level3_name
	 * @return multitype:string |unknown
	 */
	public function load($level1_name, $level2_name, $level3_name=''){
		
		$args_num = func_num_args();

		if($args_num<2 || $args_num>3){
			return array('code'=>'-9999','参数传递错误'.': ' . $module_name);
		}

		if($args_num==2){
			
			$module_name =  ucfirst($level1_name);
			$sub_module_name = strtolower($level2_name);
			$class_name = 'Usap\\Service\\'.$module_name.'\\'.$sub_module_name;
			$class_path = APP_PATH.str_replace('\\','/',$class_name);
			if(!is_file($class_path.EXT) || !class_exists($class_name)){

				return array('code'=>'-9999','加载类失败'.': ' . $class_name);
			}
			return new $class_name;
		}else{

			$module_name =  ucfirst($level1_name);
			$sub_module_name = ucfirst($level2_name);
			$lib_module_name = strtolower($level3_name);
			
			$class_name = 'Usap\\Service\\'.$module_name.'\\'.$sub_module_name. '\\' . $lib_module_name;
			
			$class_path = APP_PATH.str_replace('\\','/',$class_name);
			if(!is_file($class_path.EXT) || !class_exists($class_name)){
			
				return array('code'=>'-9999','加载类失败'.': ' . $class_name);
			}
			return new $class_name;
		}
	} 
	
	
	
	//service层级关系目录配置
	static function getModuleConfig($module_name,$sub_module_name){
		
		$module_name = ucfirst($module_name);
		$sub_module_name = strtoupper($sub_module_name);
		if($module_name=='Schedule'){
			//针对期程设定配置
			$level_info = Service::instance()->loadConfig('lottery', 'lottery','getLotteryLevel',array($sub_module_name));
			
			$class_name = strtolower($level_info[0]);
			$class_name = 'Usap\\Service\\'.$module_name.'\\'.$class_name;
			$class_path = APP_PATH.str_replace('\\','/',$class_name);
		}
		return array('class_name'=>$class_name,'class_path'=>$class_path);
	}
	
	
	
	
	public function find($module_name,$sub_module_name){
		
		$module_name = ucfirst($module_name);
		$class_name = strtolower($sub_module_name);
		
		$module_path = APP_PATH."Usap/Service/".$module_name;
		
		if(!is_dir($module_path)){//存在module
			return array('code'=>'-9999','加载module失败'.': ' . $module_name);
		}
		
		$class_name = 'Usap\\Service\\'.$module_name.'\\'.$class_name;
		$class_path = APP_PATH.str_replace('\\','/',$class_name);
		
		if(is_file($class_path.EXT) && class_exists($class_name)){
			return new $class_name;
		}else{
			$load_info = self::getModuleConfig($module_name,$sub_module_name);
			$class_name = $load_info['class_name'];
			$class_path = $load_info['class_path'];
			if(is_file($class_path.EXT) && class_exists($class_name)){
				return new $class_name;
			}
			return array('code'=>'-9999','加载服务类失败'.': ' . $module_name.'/'.strtotime($class_name));
		}
	}
	
	
	/**
	 * 载入配置类对象
	 * @param string $module_name
	 * @param string $sub_module_name
	 */
	public function loadConfig($module_name,$sub_module_name,$func_name='',$func_args_info=array()){

		$module_name = ucfirst($module_name);
		$class_name = strtolower($sub_module_name);

		$module_path = APP_PATH."Usap/Conf/".$module_name;
		if(!is_dir($module_path)){//存在module
			E('加载配置module失败'.': ' . $module_name);
		}
		$class_name = 'Usap\\Conf\\'.$module_name.'\\'.$class_name;
		$class_path = APP_PATH.str_replace('\\','/',$class_name);
		
		if(is_file($class_path.EXT) && class_exists($class_name)){
			$config_obj =  new $class_name;
			if($func_name){
				return call_user_func_array(array($config_obj,$func_name), $func_args_info);
			}
			return $config_obj;
		}else{
			E('加载配置类失败'.': ' . $module_name.'/'.strtotime($class_name));
		}
	}
	
	
	
	
	//校验日期
	static function validate_is_datetime($value){
		
		$test = @strtotime($value);
		if($test === false || $test === -1){
			return false;
		}
		return true;
	}
	
	//校验金额
	static function validate_amount($amount){
		
		if(preg_match("/^(([1-9]\d*)|0)(\.\d{1,2})?$/",$amount)){
			
			return true;
		}
		return false;
	}
	
	//校验是整数
	static function validate_is_int($value){
	
		if (strval(intval($value)) != $value)
		{
			return false;
		}
		return true;
	}
}
